//index.js
//获取应用实例
var app = getApp();
const util = require("../../../../utils/util");
// var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');

var QQMapWX = require('../../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
var postData = {};
let typeid = 0;
let allTypeList = []; // 网点类型列表

let defaultCity;
Page({
    data: {
        local: '深圳市',
        citycode: 'sz',
        province: [],
        provinceIndex: 0,
        city: [],
        cityIndex: 0,
        district: [],
        districtIndex: 0,
        showAddress: false,
        type: '网点',
        history: [],
        showPop: false,
        opentype: '',
        data: {},
        infoList: [{
                name: '办事',
                list: [{
                        name: '派出所',
                        type: 'search'
                    },
                    {
                        name: '公安局',
                        type: 'search'
                    },
                    {
                        name: '街道办',
                        type: 'search'
                    },
                    {
                        name: '出入境',
                        type: 'search'
                    },
                    {
                        name: '教育局',
                        type: 'search'
                    },
                    {
                        name: '民政局',
                        type: 'search'
                    },
                    {
                        name: '公证处',
                        type: 'search'
                    },
                    {
                        name: '社保局',
                        type: 'search'
                    },
                    {
                        name: '车管所',
                        type: 'search'
                    },
                    {
                        name: '住房建设局',
                        type: 'search'
                    },
                    {
                        name: '人力资源局',
                        type: 'search'
                    },
                    {
                        name: '政府',
                        type: 'search'
                    },
                    {
                        name: '行政服务大厅',
                        type: 'search'
                    },
                    {
                        name: '居委会',
                        type: 'search'
                    },
                    {
                        name: '社区',
                        type: 'search'
                    }
                ],
                icon: 'http://imgbdb3.bendibao.com/xcxbdb/file/20213/15/20210315160318_18252.jpeg',
                color: '#FE5E13'
            },
            {
                name: '交通',
                list: [{
                        name: '机场',
                        type: 'open'
                    },
                    {
                        name: '码头',
                        type: 'open'
                    },
                    {
                        name: '汽车站',
                        type: 'open'
                    },
                    {
                        name: '地铁站',
                        type: 'openFeature'
                    },
                    {
                        name: '火车站',
                        type: 'open'
                    }
                ],
                icon: 'http://imgbdb3.bendibao.com/xcxbdb/file/20213/15/20210315160338_92774.jpeg',
                color: '#2A83FE'
            },
            {
                name: '休闲',
                list: [{
                        name: '全部',
                        type: 'navi',
                        id: ''
                    },
                    {
                        name: '公园',
                        type: 'navi',
                        id: '5'
                    },
                    {
                        name: '寺庙',
                        type: 'navi',
                        id: '50'
                    },
                    {
                        name: '人文',
                        type: 'navi',
                        id: '69'
                    },
                    {
                        name: '休闲',
                        type: 'navi',
                        id: '72'
                    },
                    {
                        name: '展览馆',
                        type: 'navi',
                        id: '53'
                    }
                ],
                icon: 'http://imgbdb3.bendibao.com/xcxbdb/file/20213/15/20210315160240_67445.jpeg',
                color: '#6633FF'
            }
        ],
        provinceName: '默认',
        cityName: '深圳市',
        districtName: '全部',
        lat: '',
        lng: ''
    },

    onLoad: function (options) {
        ks.showLoading({
            title: '加载中',
            mask: true
        })
        util.getUrl(app.globalData.url).then(() => {
            util.request('network.php', {
                action: 'get_citys'
            }, 'POST').then(res => {
                if (res.data.state == 1) {
                    res.data.data.forEach(item => (item.chouse = false));
                    res.data.data.unshift({
                        cityname: '默认',
                        citycode: 'mr',
                        id: 0,
                        chouse: true
                    });
                    this.setData({
                        province: res.data.data,
                        city: [{
                            cityname: '深圳市',
                            id: 291,
                            citycode: 'sz'
                        }]
                    })
                    defaultCity = JSON.parse(JSON.stringify(this.data.city));
                    ks.hideLoading()
                    qqmapsdk.reverseGeocoder({
                        success: (res) => {
                            ks.showLoading({
                                title: '正在定位中',
                                mask: true
                            })
                            ks.setStorageSync('lat', res.result.location.lat)
                            ks.setStorageSync('lng', res.result.location.lng)
                            util.request('network.php', {
                                action: 'get_citys',
                                proviceid: this.data.province.filter(item => item.cityname == res.result.address_component.province)[0].id
                            }, 'POST').then(citys => {
                                let localCity = citys.data.data.filter(item => item.cityname == res.result.address_component.city)[0];
                                let city = {
                                    cityname: res.result.address_component.city,
                                    citycode: localCity.citycode,
                                    id: localCity.id,
                                    chouse: false
                                };
                                this.data.city = [city];
                                defaultCity = JSON.parse(JSON.stringify(this.data.city));
                                this.data.local = `${res.result.address_component.city}${res.result.address_component.district}`;
                                this.setData({
                                    city: this.data.city,
                                    local: this.data.local,
                                    lat: res.result.location.lat,
                                    lng: res.result.location.lng,
                                    citycode: localCity.citycode,
                                    provinceName: this.data.province[this.data.provinceIndex].cityname,
                                    cityName: this.data.city[this.data.cityIndex].cityname,
                                    districtName: res.result.address_component.district,
                                })
                                this.getDistrict(this.data.districtName);
                                ks.hideLoading()
                            })
                        },
                        fail: () => {
                            ks.hideLoading({
                                success: () => {
                                    ks.showToast({
                                        title: '定位失败',
                                        icon: 'none'
                                    })
                                }
                            })
                        }
                    })
                }
            })
        })
    },
    onShow() {
        this.data.history = ks.getStorageSync('history') ? JSON.parse(ks.getStorageSync('history')) : '';
        this.setData({
            history: this.data.history
        })

    },
    // 选择城市
    addressClick(e) {
        let index = util.getValue('index', e)
        let type = util.getValue('type', e)
        this.data[`${type}Index`] = index;
        this.data[type].forEach(item => (item.chouse = false));
        let data = this.data[type][index];
        data.chouse = true;
        this.setData({
            [`${type}Index`]: this.data[`${type}Index`],
            [type]: this.data[type]
        })
        switch (type) {
            case 'province': {
                this.setData({
                    district: []
                })
                if (data.cityname == '默认') {
                    this.setData({
                        city: defaultCity
                    })
                } else {
                    this.getCity();
                }
            }
            break;
        case 'city':
            this.getDistrict();
            break;
        case 'district': {
            this.data.showAddress = false;
            let district = this.data.district[this.data.districtIndex].id ? this.data.district[this.data.districtIndex].cityname : '全部';
            this.data.local = `${this.data.city[this.data.cityIndex].cityname}${this.data.district[this.data.districtIndex].cityname}`;
            this.setData({
                showAddress: this.data.showAddress,
                local: this.data.local,
                provinceName: this.data.province[this.data.provinceIndex].cityname,
                cityName: this.data.city[this.data.cityIndex].cityname,
                districtName: district,
                citycode: this.data.city[this.data.cityIndex].citycode
            })
        }
        break;
        default: {}
        }
    },
    // 获得城市
    getCity() {
        if (!this.data.province[this.data.provinceIndex].city)
            util.request('network.php', {
                action: 'get_citys',
                proviceid: this.data.province[this.data.provinceIndex].id
            }, 'POST').then(res => {
                if (res.data.state == 1) {
                    res.data.data.forEach(item => (item.chouse = false));
                    this.data.city = res.data.data;
                    this.data.province[this.data.provinceIndex].city = res.data.data;
                    this.setData({
                        city: this.data.city,
                        getCity: this.data.province
                    })
                }
            });
        else {
            this.data.city = this.data.province[this.data.provinceIndex].city
            this.setData({
                city: this.data.city
            })
        }

    },
    // 获得区
    getDistrict(district) {
        if (this.data.city[this.data.cityIndex].district) {
            this.data.district = this.data.city[this.data.cityIndex].district;
            this.data.districtIndex = this.data.district.findIndex(item => item.cityname == district)
            this.setData({
                district: this.data.city[this.data.cityIndex].district,
                districtIndex: this.data.districtIndex
            })
        } else {
            util.request('network.php', {
                action: 'get_citys',
                cityid: this.data.city[this.data.cityIndex].id
            }, 'POST').then(res => {
                if (res.data.state == 1) {
                    res.data.data.forEach(item => (item.chouse = false));
                    res.data.data.unshift({
                        id: '',
                        citycode: '',
                        cityname: '全市'
                    });
                    this.data.district = res.data.data;
                    this.data.city[this.data.cityIndex].district = this.data.district;
                    this.data.districtIndex = this.data.district.findIndex(item => item.cityname == district);
                    this.setData({
                        district: this.data.district,
                        city: this.data.city,
                        districtIndex: this.data.districtIndex
                    })
                }
            });
        }
    },

    itemClick(e) {
        let item = util.getValue("item", e)
        let name = item.name
        let type = item.type
        let id = item.id
        if (type == 'search') {
            ks.navigateTo({
                url: `/pages/tools/wangdian/select/select?keywords=${name}&type=1&provinceName=${this.data.provinceName}&cityName=${this.data.cityName}&districtName=${this.data.districtName}&lat=${this.data.lat}&lng=${this.data.lng}&cityCode=${this.data.citycode}`,
                success: () => util.addHistory({
                    name: name,
                    type: 1
                })
            });

        } else if (type == 'navi') {
            let areaid = this.data.district[this.data.districtIndex] ? this.data.district[this.data.districtIndex].id : '';
            ks.navigateTo({
                url: `/pages/tools/hdcx/hdcx?type=1&jingdian=${id}&citycode=${this.data.city[this.data.cityIndex].citycode}&cityname=${
						this.data.city[this.data.cityIndex].cityname
					}&areaid=${areaid}`
            });
        } else {
            ks.navigateTo({
                url: '../souvenir/souvenir?name=' + name,
            })
        }
        // if (type == 'openFeature') util.openMap(type);
        // else util.openMap('open', {name});
    },
    // 开关选择城市
    toggleAddress() {
        this.setData({
            showAddress: !this.data.showAddress
        })
    },

    onShareAppMessage: function () {
        return {
            title: '本地宝-网点查询',
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
            path: `/pages/tools/wangdian/index/index`,
        }
    },
    onPageScroll() {

    },

});